<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Cms_menus extends Admin_Controller{

	function Cms_menus(){
        parent::Admin_Controller();
        $CI = & get_instance();
        $CI->load->module_define('menus');
        $CI->load->module_model('menus.cms_menu');
	}
	
	function index(){
        $this->lists();
	}
    
    function lists(){
        $CI = & get_instance();
        
        $parent_id = 0;
        $condition = '('.CMS_MENU_PARENT_ID.' = 0 OR '.CMS_MENU_PARENT_ID.' IS NULL OR '.CMS_MENU_PARENT_ID.' = '.CMS_MENU_ID.')';
        
        if($CI->uri->total_segments()>=3){
            $parent_id = $CI->uri->segment(3);
            // Check If Child manu is available
            $CI->cms_menu->set_id($parent_id);
            if($CI->cms_menu->get()){
                // Child menu is available
                $condition = CMS_MENU_PARENT_ID.' = '.$parent_id;
            }
        }
        $base_url = 'admin/menus.cms_menus';

        list($limit, $offset, $sort_field, $sort_type) = array($CI->config->item('rows_per_page'), 4, 5 ,6);

        $this->_lists($condition, $base_url.'/'.$parent_id, $limit, $offset, $sort_field, $sort_type);
        if($CI->cms_menu->is_loaded()){
            $parents = $CI->cms_menu->parent_recursive();
            $s = '<a href="'.site_url($base_url).'">'.get_class($this).'</a>';
            foreach($parents as $menu_item){
                $s .= ' > <a href="'.site_url($base_url).'/'.$menu_item->id.'">'.h($menu_item->name).'</a>';
            }
            $s .= ' > '.$CI->cms_menu->name;
            $this->module_navigation($s);
        }
        $this->module_toolbar('menus.cmsmenu_toolbar', array('parent_id' => $parent_id));
        $this->view('cmsmenu_lists');
    }

    function _lists($condition, $base_url, $limit, &$offset=0, &$sort_field='name', &$sort_type='0'){
        $CI = & get_instance();
        $config['uri_segment'] = $offset;
        $offset = $CI->uri->segment($offset)?$CI->uri->segment($offset):0;
        $sort_field = $CI->uri->segment($sort_field)?$CI->uri->segment($sort_field):'name';
        $sort_type = $CI->uri->segment($sort_type)?'1':'0';

        $menus = $CI->cms_menu->get_lists($condition, $limit, $offset, $sort_field, $sort_type);
        $this->add('menus', $menus);

        $CI->load->library('pagination');
        $config['base_url'] = site_url($base_url.'/%s/'.$sort_field.'/'.$sort_type);
        $config['total_rows'] = $CI->cms_menu->get_numrows();
        $config['per_page'] = $limit;
        $CI->pagination->initialize($config);
        $this->add('paging', $CI->pagination->create_links());
        
        $this->add('NameTX', $this->sort_column('Name', 'name', $sort_type, $base_url.'/'.$offset, $sort_field));
        $this->add('OrderTX', $this->sort_column('Order', 'ordering', $sort_type, $base_url.'/'.$offset, $sort_field));
        $this->add('EnableTX', $this->sort_column('Enable', 'enabled', $sort_type, $base_url.'/'.$offset, $sort_field));
    }
    
    function add_new(){
        $CI = & get_instance();
        $CI->load->module_define('users');
        $CI->load->module_language('menus.add');
        $CI->load->library('form_validation');
        $CI->load->helper('form');
        $CI->form_validation->set_rules('name', 'Name', 'trim|required');
		$CI->form_validation->set_rules('ordering', 'Order', 'trim|required|integer');

        $parent_id = $CI->input->post('parent_id')?$CI->input->post('parent_id'):$CI->uri->segment(3);
        if ($CI->form_validation->run() !== FALSE){
            $data = array(CMS_MENU_NAME => $CI->input->post('name'),
                          CMS_MENU_LINK => $CI->input->post('link'),
                          CMS_MENU_ORDERING => $CI->input->post('ordering'),
                          CMS_MENU_ENABLED => $CI->input->post('enabled')?'1':'0',
                          CMS_MENU_PARENT_ID => $CI->input->post('parent_id'),
                          CMS_MENU_PARAM => $CI->input->post('param'),
                          );
            if($CI->cms_menu->save($data)){
                set_flash_message(lang('menu_add_success'));
                redirect('admin/menus.cms_menus/'.$parent_id);
            }
        }

        $menus = $CI->cms_menu->menu_list_tree();
        $menu_tree = $CI->parser->module_parse('menus.menu_tree', array('menus' => $menus,'parent_id' => $parent_id), TRUE);
        $this->add('menu_tree', $menu_tree);

        $this->add('parent_id', $parent_id);
        $this->add('enabled', $CI->input->post('enabled'));
        $this->add('name', $CI->input->post('name'));
        $this->add('link', $CI->input->post('link'));
        $this->add('ordering', $CI->input->post('ordering'));
        $this->add('param', $CI->input->post('param'));
        
        $this->module_navigation(get_class($this).' : '.lang('menu_add_title'));
        $this->view('cmsmenu_add_new');
    }
    
    function edit(){
        $CI = & get_instance();
        $CI->load->module_language('menus.edit');
        $menu_id = $CI->uri->segment(3);
        $CI->cms_menu->set_id($menu_id);
        if(!$CI->cms_menu->get()){
            set_flash_message(lang('menu_edit_not_found'));
            redirect('admin/menus.cms_menus/');
        }
        
        $CI->load->module_define('users');
        $CI->load->library('form_validation');
        $CI->load->helper('form');
        $CI->form_validation->set_rules('name', 'Name', 'trim|required');
		$CI->form_validation->set_rules('ordering', 'Order', 'trim|required|integer');

        if ($CI->form_validation->run() !== FALSE){
            $data = array(CMS_MENU_NAME => $CI->input->post('name'),
                          CMS_MENU_LINK => $CI->input->post('link'),
                          CMS_MENU_ORDERING => $CI->input->post('ordering'),
                          CMS_MENU_ENABLED => $CI->input->post('enabled')?'1':'0',
                          CMS_MENU_PARENT_ID => $CI->input->post('parent_id'),
                          CMS_MENU_PARAM => $CI->input->post('param'),
                          );
            if($CI->cms_menu->save($data)){
                set_flash_message(lang('menu_edit_success'));
                redirect('admin/menus.cms_menus/'.$CI->input->post('parent_id'));
            }
        }

        $menus = $CI->cms_menu->menu_list_tree();
        $menu_tree = $CI->parser->module_parse('menus.menu_tree', array('menus' => $menus,'parent_id' => ($CI->input->post('parent_id')!='')?$CI->input->post('parent_id'):$CI->cms_menu->parent_id), TRUE);
        $this->add('menu_tree', $menu_tree);

        $this->add('parent_id', ($CI->input->post('parent_id')!='')?$CI->input->post('parent_id'):$CI->cms_menu->parent_id);
        $this->add('enabled', ($CI->input->post('enabled'))?$CI->input->post('enabled'):$CI->cms_menu->enabled);
        $this->add('name', ($CI->input->post('name'))?$CI->input->post('name'):$CI->cms_menu->name);
        $this->add('link', ($CI->input->post('link'))?$CI->input->post('link'):$CI->cms_menu->link);
        $this->add('ordering', ($CI->input->post('ordering'))?$CI->input->post('ordering'):$CI->cms_menu->ordering);
        $this->add('param', ($CI->input->post('param'))?$CI->input->post('param'):$CI->cms_menu->param);
        
        $this->module_navigation(get_class($this).' : '.lang('menu_edit_title'));
        $this->view('cmsmenu_add_new');
    }
    
    function switch_status(){
        $CI = & get_instance();
        if(!$CI->uri->segment(3)){
            echo '';
            return;
        }

        $CI->cms_menu->set_id($CI->uri->segment(3));
        if(!$CI->cms_menu->get()){
            echo '';
            return;
        }
        
        $current_status = $CI->cms_menu->enabled;
        $new_status = ($current_status=='1')?'0':'1';
        $CI->cms_menu->save(array(CMS_MENU_ENABLED => $new_status));
        echo $new_status;
    }
}